package com.flyox.game.militarychess.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class SerialUtil {
	public static byte[] enSerial(Serializable message) throws IOException {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		ObjectOutputStream oos = new ObjectOutputStream(baos);
		oos.writeObject(message);
		byte[] data = baos.toByteArray();
		oos.close();
		return data;
	}

	public static Serializable deSerial(byte[] bytes) throws Exception {
		ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
		ObjectInputStream ois = new ObjectInputStream(bais);
		Serializable ser = (Serializable) ois.readObject();
		ois.close();
		return ser;
	}
	
	public static byte[] intToByteArray (final int integer) throws IOException {	
	      ByteArrayOutputStream bos = new ByteArrayOutputStream();
	      DataOutputStream dos = new DataOutputStream(bos);
	      dos.writeInt(integer);
	      dos.flush();
	      return bos.toByteArray();
	}
}
